<!-- 待约面-约面 -->
<template>
  <BasicModal :show="probationShow" :title="type === 'zrsy' ? '转入试用期111' : '转正'" width="30%" :widthComfirm="true"
    @comfirm="probationComfirm" @closeModal="closeModal">
    <template #content>
      <CommonForm :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>
</template>

<script setup lang='ts'>
import { ref } from "vue"
import CommonForm from "@/views/applicationDetails/CommonForm.vue";
import BasicModal from "./BasicModal.vue";
import type { IndexFormItem } from "@/views/ApplicationDetails/application-type.ts";
import { message } from "ant-design-vue";
import {
  selectCommonConfigDetail, probationProgress, internshipProgress, selectCommonConfigFoChildren,
  selectUserByRole, getEntrySchoolInfo
} from "@/api/application-details/index.ts";
import { comStores } from '@/store/common/comStore';
const store = comStores()
const probationShow = ref<boolean>(false);
const commentShow = ref<boolean>(false)
const dimissionShow = ref<boolean>(false);
const paramsList = () => ([
  { label: "试用", name: "cascader", value: [], component: 'cascader', options: [] },
  { label: "导师", name: "entryTeacher", value: "", component: 'select', options: [], required: false, show: true },
  // { label: "入职科目", name: "entrySubject", value: "", component: 'select', options: [] },
  // { label: "入职年级", name: "entryGrade", value: "", component: 'select', options: [] },
  { label: "备注", name: "remark", value: "", component: 'textarea', required: false },
])
const formList = ref<IndexFormItem[]>(paramsList())

const getOptions = async () => {
  const school = await selectCommonConfigFoChildren({ type: 1 }); //入职分校
  const teacher = await selectUserByRole("MENTOR") //导师
  formList.value[1].options = teacher.data
  // const school = await selectCommonConfigDetail({ type: "BRANCH_SCHOOL" }); //入职分校
  // const subject = await selectCommonConfigDetail({ type: "SUBJECT" }) //科目
  // const grade = await selectCommonConfigDetail({ type: "GRADE" }) //科目
  formList.value[0].options = school.data

}

const emits = defineEmits(['onSuccess'])
const comment = () => {
  commentShow.value = true
  console.log('comment 试用中')
}
const dimission = () => {
  dimissionShow.value = true
  console.log('quitPost 试用中')
}

const dieout = () => {
  emits('dieOut', '试用中')
  console.log('dieout 试用中')
}

const surrender = () => {
  console.log('surrender 试用中')
  emits('surrender', '试用中')
}


const probation = () => {
  probationShow.value = true
  getOptions()
  console.log('probation', '试用中')
}
const formRef = ref<any>(null)
const probationComfirm = async () => {
  const res = await formRef.value.submitForm();
  if (!res) return;
  let param: any = {
    editStatus: type.value === 'zrsy' ? 'TRYOUT' : 'CONFIRMED'
  }
  console.log(res, 'res')
  res.forEach(el => {
    param[el.name] = el.value
  })
  if (type.value === 'zrsy') {
    param.entrySchool = res[0].value[0]
    param.entrySubject = res[0].value[1]
    param.entryGrade = res[0].value[2]
  } else {
    param.tryoutSchool = res[0].value[0]
    param.tryoutSubject = res[0].value[1]
    param.tryoutGrade = res[0].value[2]
  }
  console.log(param, "====>")
  const data = await send(param);
  if (!data) return
  formList.value.forEach(el => el.value = "")
}
const send = async (param) => {
  param.resumeId = info.value.id
  param.processId = info.value.processId
  param.operator = info.value.operator || '操作人'
  const data = type.value === 'zrsy' ? await internshipProgress(param) : await probationProgress(param);
  if (data && data.code === '10000') {
    message.success("操作成功")
    emits('onSuccess')
    formList.value = paramsList()
    probationShow.value = false
    return true
  } else {
    message.error("操作失败")
    return false
  }
}
const info = ref<any>({});
const type = ref<any>();
const open = async (params: any) => {
  info.value = params.info
  type.value = params.type
  console.log(params)
  // if(type.value === 'zz'){
  // formList.value[0].name = 'tryoutSchool'
  // formList.value[1].name = 'tryoutSubject'
  // formList.value[2].name = 'tryoutGrade'
  // formList.value[3].name = 'remark'
  // }
  let typeIndex = 0;
  // type.value === 'zrsy' ? 0 : 1
  if (type.value === 'zz') {
    typeIndex = 2
  } else if (type.value === 'zrsy') {
    typeIndex = 1
  }else{
    typeIndex = 0
  }
  const data = await getEntrySchoolInfo({ resumeId: params.info.id, type: typeIndex });
  const schoolInfo = data.data ? data.data : {}
  const { school, subject, grade, teacher } = schoolInfo;
  console.log(params.info, 'params.info')
  formList.value[1].value = teacher
  formList.value[0].value = [school, subject, grade]
  // formList.value[0].value = params.info.entryLocation
  // formList.value[1].value = params.info.entrySubject
  // formList.value[2].value = params.info.entryGrade
  formList.value[0].label = type.value === 'zrsy' ? "试用" : "转正"
  formList.value[1].show = type.value === 'zrsy'
  probation()
}
const closeModal = () => {
  probationShow.value = false
  formList.value = paramsList()
  formRef.value.clear()
}
defineExpose({ open })

</script>
<style lang="less" scoped></style>
